<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据驱动</title>
</head>

<body>
    <div>
        <div>商品数量<input id="goodsnum" /></div>
        <div>商品价格<input id="goodsprice" /></div>
        <div>小计<span id="total"></span></div>
    </div>
</body>
<script>
    let goodsnum = document.getElementById("goodsnum");
    let goodsprice = document.getElementById("goodsprice");
    let total = document.getElementById("total");

    let goodsinfo = {};
    var _goodsnum = 0;
    var _goodsprice = 0;
    Object.defineProperty(goodsinfo, "goodsnum", {
        set(n) { //对象设值的时候
            // console.log("设值");
            _goodsnum = n;
            complier();
        },
        get() { //对象取值的时候
            // console.log("取值");
            return _goodsnum;
        }
    })
    Object.defineProperty(goodsinfo, "goodsprice", {
        set(n) {
            _goodsprice = n;
            complier();
        },
        get() {
            return _goodsprice;
        }
    })

    function complier() {
        total.innerHTML = goodsinfo.goodsnum * goodsinfo.goodsprice;
    }

    goodsnum.oninput = function () {
        goodsinfo.goodsnum = this.value;
    }
    goodsprice.oninput = function () {
        goodsinfo.goodsprice = this.value;
    }





</script>

</html>